<?
/**********************************************
Created by : G.M.Sundar 
[created : 01/17/2007, 09:17:59 AM]
 
Description: Used to enter and edit GSP Data

***********************************************/
include("../../includes/global_files/tables.inc");
include(DOCINCLUDES."global_files/global_html.inc");
include(DOCINCLUDES."global_files/global_form.inc");//
include(DOCINCLUDES."global_files/global_report.inc");

include(DOCINCLUDES."global_files/global_pgsql.inc");
include(DOCINCLUDES."global_files/global_functions.inc");
 include(DOCINCLUDES."global_files/global_session.inc");


$page=new cPage;
$page->header_path="../../";

$form=new cForm();
$db=new cDatabase($company_id);
$report=new cReport();
$sql=new pgSql();//

if($_GET)
{

$buyer=$_GET['buyer'];
$account=$_GET['account'];
$action=$_GET['action'];
if($action=="edit")
$gsp_id=$_GET['gsp_id'];
}
if($_POST)
{
$action=$_POST['action'];
$gsp_id=$_POST['gsp_id'];

}


if($_POST)
{
for($i=1;$i<=15;$i++)
 {
	if($_POST['des_'.$i])
	{
		$data_array[$i]['item_no']=$_POST['item_'.$i];
		$data_array[$i]['marks']=$_POST['marks_'.$i];
// 		if($action=='edit')
// 		$data_array[$i]['description']=str_replace('. ','.\\n',($_POST['des_'.$i]));
// 		else
		$data_array[$i]['description']=str_replace('. ','.\\n',($_POST['des_'.$i]));
		$data_array[$i]['origin']=$_POST['origin_'.$i];
		$data_array[$i]['weight']=$_POST['weight_'.$i];
		$data_array[$i]['nos']=$_POST['nos_'.$i];
		$data_array[$i]['item_id']=$_POST['item_id_'.$i];
	}
 }

if(count($data_array)>=1)
	{

$db->beginTransaction();


 	$gsp_commercial_array["buyer_id"]=$_POST['buyer'];
 	$gsp_commercial_array["account_id"]=$_POST['account'];
	if($_POST['ship_mode'])
	$gsp_commercial_array["ship_mode_id"]=$_POST['ship_mode'];
	if($_POST['location'])
	$gsp_commercial_array["ship_from_city"]=$_POST['location'];
	if($_POST['countryfrom'])
	$gsp_commercial_array["ship_from_country"]=$_POST['countryfrom'];
	if($_POST['countryto'])
	$gsp_commercial_array["ship_to_country"]=$_POST['countryto'];
	if($_POST['port'])
	$gsp_commercial_array["ship_to_port"]=$_POST['port'];
	if($_POST['ship_date'])
	$gsp_commercial_array["ship_date"]=getFormattedDate($_POST['ship_date']);

	$gsp_commercial_array["remarks"]=$_POST['remarks'];//

	
	$gsp_commercial_array["support_documents"]=$_POST['support_documents'];
	$gsp_commercial_array["origin_requirements"]=str_replace('. ','.\\n',($_POST['origin_requirements']));//
	$gsp_commercial_array["book_nos"]=$_POST['book_nos'];//
	$gsp_commercial_array["ship_status"]=$_POST['ship_status'];//
	$gsp_commercial_array["signatory"]=$_POST['signatory_'];//
	$gsp_commercial_array["pre_carriage_mode"]=$_POST['precarriagemode'];//
	$gsp_commercial_array["pre_carriage_from"]=$_POST['precarriagefrom'];//
	$gsp_commercial_array["pre_carriage_to"]=$_POST['precarriageto'];//
	
	$sql->table = $gsp_commercial_details_table;
	$sql->column = $gsp_commercial_array;
	if($action=='edit')
		{
		$condition_array['gsp_id']="gsp_id=".$gsp_id;
  		$sql->condition =$condition_array;
  		$sql->generateUpdateQuery();
		}
	else
		{
		$sql->generateInsertQuery();
		}

	 $db->query = $sql->query;
	 $db->runQuery();

       if($action!='edit')
		{
		$db->query="Select max(gsp_id)as gsp_id from
	$gsp_commercial_details_table";
		$db->runQuery();
		$id_array=$db->returnArrays();
		$gsp_id=$id_array[0]['gsp_id'];
		}

	foreach($data_array as $key=>$value)
		{
		$gsp_item_details_array["gsp_id"]=$gsp_id;
		$gsp_item_details_array["item_no"]=$value['item_no'];
		$gsp_item_details_array["marks_nos_packages"]=$value['marks'];
		$gsp_item_details_array["description"]=$value['description'];
		$gsp_item_details_array["origin"]=$value['origin'];
		$gsp_item_details_array["gross_weight"]=$value['weight'];
		$gsp_item_details_array["nos_invoice_date"]=$value['nos'];

		$sql->table = $gsp_item_details_table;
		$sql->column = $gsp_item_details_array;

       if($action=='edit')
		{
		$condition_array['item_id']="item_id=".$value['item_id'];
		$sql->condition =$condition_array;
		$sql->generateUpdateQuery();
		}
	else
		{
		$sql->generateInsertQuery();
		}
		$db->query = $sql->query;
		$db->runQuery();
		}
$db->endTransaction();
unset($_POST);
	header("location:gsp_details.php?gsp_id=$gsp_id");
exit;
	}


}
if($action=="edit")
$title="Edit Page for GSP id - $gsp_id";
else
$title="GSP Entry Page";

$page->title=$page->heading="$title";
$page->doHeaderHtml();


// $page->title=$page->heading="Genrealized System of Perferences";
$form->properties=array("name"=>"gspform","method"=>"post","action"=>$_SERVER
["PHP_SELF"]);
echo $form->startForm();
$gsp_commercial_details_array=array();
if($action=='edit')
{

$db->query="SELECT account_id,buyer_id,ship_mode_id,ship_from_city,ship_from_country,ship_to_country,ship_to_port,to_char(ship_date,'dd/mm/yyyy') as ship_date,remarks,support_documents,book_nos,origin_requirements,ship_status,signatory,pre_carriage_mode,pre_carriage_from,pre_carriage_to from $gsp_commercial_details_table where gsp_id =$gsp_id ";
$db->runQuery();
$gsp_commercial_details_array=$db->returnArrays();

$account=$gsp_commercial_details_array[0]['account_id'];

 $buyer=$gsp_commercial_details_array[0]['buyer_id'];
}


 $db->query="SELECT account_name,1,concat(company_name,'-',account_code) as company_name from $_jaccount_table ja join
$_mcompany_table mc on mc.company_id=ja.account_name order by
3;";
$db->runQuery();
$account_array=$db->returnArrays();


$form->properties=array("name"=>"account","class"=>"textm","onChange"=>"self.
location.href=\"gsp.php?account=\"+this.value");
	$form->select_tag_properties=array("default"=>"$account","value"=>$_GET[
"account"],"arrays"=>$account_array);
$gspform_data[0]["Exporter Name"]=$form->generateSelectTag();



if($account)
{

 $db->query="SELECT company_name as \"display_exporter\",
address,cc.city_name
as
\"city_id\",cc.country_name
as countryfrom,mc.city_name,ms.state_name,mco.country_name,zipcode from
$company_contact_table cc left join $_mcity_table mc on mc.city_id=cc.city_name left join
$_mcountry_table mco on mco.country_id=cc.country_name left join $_mstate_table ms on
ms.state_id=cc.state where company_id =$account";	
$db->runQuery();
$account_contact_array=$db->returnArrays();

$gspform_data[0]["ExporterName"]=$account_contact_array[0]['display_exporter'];
$gspform_data[0]["Exporter Address"]=$account_contact_array[0]['address'];
$gspform_data[0]["Exporter City"]=$account_contact_array[0]['city_name'];
$gspform_data[0]["Exporter State"]=$account_contact_array[0]['state_name'];
$gspform_data[0]["Exporter Country"]=$account_contact_array[0]['country_name'];
$gspform_data[0]["Exporter Zip code"]=$account_contact_array[0]['zipcode'];
$gspform_data[0]["Exporter Address"]=$account_contact_array[0]['address'];




if($action=='edit')
		$buyer=$gsp_commercial_details_array[0]['buyer_id'];


$db->query="SELECT buyer_name,1,concat(company_name,'-',buyer_code) as company_name from $_jbuyer_table jb join
$_mcompany_table mc on mc.company_id=jb.buyer_name order by 3;";
$db->runQuery();
$buyer_array=$db->returnArrays();

$form->properties=array("name"=>"buyer","class"=>"textm","onChange"=>"self.
location.href=\"gsp.php?account=$account&buyer=\"+this.value");
	$form->select_tag_properties=array("default"=>"$buyer","value"=>$buyer,"arrays"=>$buyer_array);
$gspform_data[0]["Buyer"]=$form->generateSelectTag();


if($buyer)
{

$db->query="SELECT company_name as \"display_buyer\",
address,cc.country_name as countryto,cc.city_name
as \"city_id\",mc.city_name,ms.state_name,mco.country_name,zipcode
from $company_contact_table cc left join $_mcity_table mc on mc.city_id=cc.city_name
left join $_mcountry_table mco on mco.country_id=cc.country_name left join $_mstate_table
ms on ms.state_id=cc.state where company_id = $buyer ";		
$db->runQuery();
$buyer_contact_array=$db->returnArrays();

$gspform_data[0]["BuyerName"]=$buyer_contact_array[0]['display_buyer'];
$gspform_data[0]["Address"]=$buyer_contact_array[0]['address'];
$gspform_data[0]["City"]=$buyer_contact_array[0]['city_name'];
$gspform_data[0]["State"]=$buyer_contact_array[0]['state_name'];
$gspform_data[0]["Country"]=$buyer_contact_array[0]['country_name'];
$gspform_data[0]["Zip code"]=$buyer_contact_array[0]['zipcode'];
$gspform_data[0]["Address"]=$buyer_contact_array[0]['address'];

$db->query="SELECT shipment_mode_id,1,shipment_mode_name from
$_mship_mode_table order by 3;";
$db->runQuery();
$ship_mode_array=$db->returnArrays();

if($action=='edit')
$pre_carriage_mode=$gsp_commercial_details_array[0]['pre_carriage_mode'];

$form->properties=array("name"=>"precarriagemode");
$form->select_tag_properties=array("default"=>$pre_carriage_mode,"arrays"=>$ship_mode_array,"restore_default"=>$_POST[
"pre_carriage_mode"]);
$gspform_data[0]["Pre Carriage Mode"]=$form->generateSelectTag();

$db->query="SELECT city_id,1,city_name from $master_city_table order by 3;";
$db->runQuery();
$location_array=$db->returnArrays();

if($action=='edit')
$pre_carriage_from=$gsp_commercial_details_array[0]['pre_carriage_from'];

$form->properties=array("name"=>"precarriagefrom");
$form->select_tag_properties=array("default"=>$pre_carriage_from,"arrays"=>$location_array,"restore_default"=>$_POST[
"precarriagefrom"]);
$gspform_data[0]["Pre Carriage From"]=$form->generateSelectTag();


if($action=='edit')
$pre_carriage_to=$gsp_commercial_details_array[0]['pre_carriage_to'];

$form->properties=array("name"=>"precarriageto");
$form->select_tag_properties=array("default"=>$pre_carriage_to,"arrays"=>$location_array,"restore_default"=>$_POST[
"precarriageto"]);
$gspform_data[0]["Pre Carriage To"]=$form->generateSelectTag();


if($action=='edit')
$ship_from_city=$gsp_commercial_details_array[0]['ship_from_city'];


$form->properties=array("name"=>"location");
$form->select_tag_properties=array("default"=>$ship_from_city,"arrays"=>$location_array,"restore_default"=>$_POST[
"location"]);
$gspform_data[0]["City From"]=$form->generateSelectTag();

$db->query="SELECT country_id,1,country_name from $_mcountry_table order by
1;";	
$db->runQuery();
$country_array=$db->returnArrays();

if($action=='edit')
$ship_from_country=$gsp_commercial_details_array[0]['ship_from_country'];

$form->properties=array("name"=>"countryfrom");
$form->select_tag_properties=array("default"=>$ship_from_country,"arrays"=>$country_array,"restore_default"=>$_POST[
"countryfrom"]);
$gspform_data[0]["Country From"]=$form->generateSelectTag();


$country_to=($action=='edit')?$gsp_commercial_details_array[0]['ship_to_country']:$buyer_contact_array[0]["countryto"];
$form->properties=array("name"=>"countryto");
$form->select_tag_properties=array("default"=>$country_to,"arrays"=>$country_array,"restore_default"=>$_POST[
"countryto"]);
$gspform_data[0]["Country To"]=$form->generateSelectTag();


if($action=='edit')
$shipment_mode_id=$gsp_commercial_details_array[0]['ship_mode_id'];



$form->properties=array("name"=>"ship_mode");
$form->select_tag_properties=array("default"=>$shipment_mode_id,"arrays"=>$ship_mode_array,"restore_default"=>$_POST[
"ship_mode"]);
$gspform_data[0]["Ship Mode"]=$form->generateSelectTag();

$db->query="SELECT port_id,1,port_name from $_mport_table order by 3;";
$db->runQuery();
$port_array=$db->returnArrays();

if($action=='edit')
$ship_to_port=$gsp_commercial_details_array[0]['ship_to_port'];


$form->properties=array("name"=>"port");
$form->select_tag_properties=array("default"=>$ship_to_port,"arrays"=>$port_array,"restore_default"=>$_POST[
"port"]);
$gspform_data[0]["Port"]=$form->generateSelectTag();



$now=date("d/m/Y");
$_POST["ship_date"]=($gsp_commercial_details_array[0]['ship_date'])?$gsp_commercial_details_array[0]['ship_date']:$now;

$form->properties=array("type"=>"text","name"=>"ship_date","value"=>
$_POST["ship_date"],"size"=>"9","maxlength"=>"10","class"=>"datem");
$gspform_data[0]["Date (dd/mm/yyyy)"]=$form->generateInputTag();

$gspform_data[0]["Date (dd/mm/yyyy)"].="<a
href=\"javascript:show_calendar('gspform.ship_date');\"
onmouseover=\"window.status='Date Picker';return true;\"
onmouseout=\"window.status='';return true;\"><img
src=\"".$page->header_path."images/calendar.gif\" width=\"16\"
height=\"16\" border=\"0\"></a>";
$gspform_data[0]["Date (dd/mm/yyyy)"].=$dv->errors["issue_date"];

$support_documents=$gsp_commercial_details_array[0]['support_documents']?$gsp_commercial_details_array[0]['support_documents']:$_POST['support_documents'];
$form->properties =
array("name"=>"support_documents","value"=>$support_documents,"size"=>"50",
"maxlength"=>"250","class"=>"textn");
$gspform_data[0]["F.O.B Value"]=$form->generateInputTag();
if($action=='edit')
$remarks=$gsp_commercial_details_array[0]['remarks'];
else
$remarks=$gsp_commercial_details_array[0]['remarks']?$gsp_commercial_details_array[0]['remarks']:"No.49,PULIAMBEDU NOOMBAL POST,GOPARASANALLUR VILLAGE,CHENNAI";
$form->properties =
array("name"=>"remarks","value"=>$remarks,"size"=>"50",
"maxlength"=>"250","class"=>"textn");
$gspform_data[0]["Factory Address"]=$form->generateInputTag();
if($action=='edit')
$origin_requirements=$gsp_commercial_details_array[0]['origin_requirements'];
else
$origin_requirements=$gsp_commercial_details_array[0]['origin_requirements']?$gsp_commercial_details_array[0]['origin_requirements']:"GOODS ARE WHOLLY OBTAINED IN INDIA \n GOODS YET NOTSHIPPED";
$form->properties =
array("name"=>"origin_requirements","value"=>$origin_requirements,"size"=>"50",
"maxlength"=>"250","class"=>"textn");
$gspform_data[0]["Goods Status"]=$form->generateInputTag();
$gsp_item_details_array=array();

if($action=='edit')
$book_nos=$gsp_commercial_details_array[0]['book_nos'];
else
$book_nos=$gsp_commercial_details_array[0]['book_nos']?$gsp_commercial_details_array[0]['book_nos']:"";
$form->properties =
array("name"=>"book_nos","value"=>$book_nos,"size"=>"50",
"maxlength"=>"350","class"=>"textn","title"=>"Use '~' to split book nos in the sequence they are used");
$gspform_data[0]["Book Nos"]=$form->generateInputTag();

if($action=='edit')
$ship_status=$gsp_commercial_details_array[0]['ship_status'];
else
$ship_status=$gsp_commercial_details_array[0]['ship_status']?$gsp_commercial_details_array[0]['ship_status']:"";

$form->properties =
array("name"=>"ship_status","value"=>$ship_status,"size"=>"50",
"maxlength"=>"350","class"=>"textn");
$gspform_data[0]["Shipment Status"]=$form->generateInputTag();
$gsp_item_details_array=array();

if($action=='edit')
$signatory=$gsp_commercial_details_array[0]['signatory'];
else
$signatory=$gsp_commercial_details_array[0]['signatory']?$gsp_commercial_details_array[0]['signatory']:"";

$form->properties =
array("name"=>"signatory_","value"=>$signatory,"size"=>"50",
"maxlength"=>"350","class"=>"textn");
$gspform_data[0]["Authorised Signatory"]=$form->generateInputTag();
$gsp_item_details_array=array();

if($action=='edit')
{
$db->query="SELECT item_id,item_no,marks_nos_packages,description,origin,gross_weight,nos_invoice_date from $gsp_item_details_table where gsp_id=$gsp_id";	
$db->runQuery();
$gsp_item_details_array=$db->returnArrays();
}

$j=0;
for($i=1;$i<=15;$i++)
{
$item_no[$i]=$gsp_item_details_array[$i-1]['item_no']?$gsp_item_details_array[$i-1]['item_no']:$_POST['item_'.$i];

$form->properties =
array("name"=>"item_".$i,"value"=>$item_no[$i],
"size"=>"5", "maxlength"=>"5","class"=>"textn","TABINDEX"=>"$j");
$gspitem_data[0]["Item number"].= $form->generateInputTag();
$j++;
$marks[$i]=$gsp_item_details_array[$i-1]['marks_nos_packages']?$gsp_item_details_array[$i-1]['marks_nos_packages']:$_POST['marks_'.$i];
$form->properties =
array("name"=>"marks_".$i,"value"=>$marks[$i],"size"=>"20","class"=>
"textn","TABINDEX"=>"$j");
$gspitem_data[0]["Marks and number of Packages"].=$form->generateInputTag();
$j++;
$description[$i]=$gsp_item_details_array[$i-1]['description']?$gsp_item_details_array[$i-1]['description']:$_POST['des_'.$i];
$form->properties =
array("name"=>"des_".$i,"value"=>$description[$i],"size"=>"100","class"=>
"textn","TABINDEX"=>"$j");
$gspitem_data[0]["Number and kind of Packages; Description of goods"].=
$form->generateInputTag();
$j++;
$origin[$i]=$gsp_item_details_array[$i-1]['origin']?$gsp_item_details_array[$i-1]['origin']:$_POST['origin_'.$i];
$form->properties =
array("name"=>"origin_".$i,"value"=>$origin[$i],"size"=>"20","class"=>
"textn","TABINDEX"=>"$j");
$gspitem_data[0]["Origin Criterion"].= $form->generateInputTag();
$j++;
$weight[$i]=$gsp_item_details_array[$i-1]['gross_weight']?$gsp_item_details_array[$i-1]['gross_weight']:$_POST['origin_'.$i];
$form->properties =
array("name"=>"weight_".$i,"value"=>$weight[$i],"size"=>"20","class"=>
"textn","TABINDEX"=>"$j");
$gspitem_data[0]["Gross Weight or Other Quantity"].=
$form->generateInputTag();
$j++;
$nos[$i]=$gsp_item_details_array[$i-1]['nos_invoice_date']?$gsp_item_details_array[$i-1]['nos_invoice_date']:$_POST['nos_'.$i];
$form->properties =
array("name"=>"nos_".$i,"value"=>$nos[$i],"size"=>"20","class"=>"textn"
,"TABINDEX"=>"$j") ;
$gspitem_data[0]["Number & Date of invoices"].= $form->generateInputTag();
$j++;
$form->properties=array("type"=>"hidden","name"=>"item_id_$i","value"=>$gsp_item_details_array[$i-1]['item_id']);
echo $form->generateInputTag();
}
if($action=='edit')
$button_text="Edit";
else
$button_text="Insert";
$form->properties=array("type"=>"button","name"=>"insert","value"=>"$button_text",
"onClick"=>"showConfirmBox('gsp.php','gspform');");
$gsp_save[0][""]="<BR>".$form->generateInputTag();

$form->properties=array("type"=>"hidden","name"=>"gsp_id","value"=>"$gsp_id");
echo $form->generateInputTag();
$form->properties=array("type"=>"hidden","name"=>"action","value"=>"$action");
echo $form->generateInputTag();
}
}


$report->data=$gspform_data;
$report->title="GSP Entry Page";
$report->customization["table_style"]="width:40%;";
echo "<div align=\"center\">";
$report->doVerticalTable();
echo "</div>";

$report->data=$gspitem_data;
$report->title="";
$report->customization["table_style"]="width:50%;
backcolour:white;border-style:none; border-width:none;
border-collapse:collapse";
$report->customization["tr_style"]="border-style:none;
border-width:thin; border-collapse:collapse";
$report->customization["column_style"][""]="text-align:center";
$report->highlight=true;
$report->doHorizontalTable();

$report->data=$gsp_save;
$report->title="";
$report->customization["table_style"]="width:100%;
backcolour:white;border-style:none; border-width:none;
border-collapse:collapse";
$report->customization["tr_style"]="border-style:none;
border-width:none";

$report->highlight=False;
$report->doHorizontalTable();
echo $form->endForm();

?>
<script language="javascript">

function showConfirmBox(file_name,form_name)
{
var ok = confirm("Do you want to save");
if(ok)
{
document.gspform.action="gsp.php";
document.gspform.submit();
}
}
</script>